//
//  channel.hpp
//  Wi-Fi Party remade-1
//
//  Created by Vieta Ken on 2021/5/29.
//

#ifndef channel_hpp
#define channel_hpp

#include "packet.hpp"
#include "slab.hpp"
#include <functional>
#include <list>
#include <map>
class Receiver;

enum type{
    channel_reliable,
    channel_unreliable
};

void ensure_sent(Packet* packet,std::map<int,std::map<unsigned int, bool>*> *known_hosts,std::function<void()> send_packet);
class Channel{
public:
    unsigned int channel_id;
    int socket;
    enum type type;
    unsigned int resend;
    unsigned int index;
    //std::list<bool (*)(Packet* packet)> handlers;
    std::list<std::function<bool(Packet* received_packet)>> *handlers;
    Receiver *receiver;


    //Channel(unsigned int channel_id,enum type type,unsigned int resend, int socket, bool (*handler)(Packet *packet));
    Channel(unsigned int channel_id,enum type type,unsigned int resend, int socket, std::function<bool(Packet*)> handler);
    void send(const void* content, unsigned int length, unsigned int delay);
    void receive_packet(Packet* packet);

};


#endif /* channel_hpp */
